package com.dao.impl;

import com.dao.Order;
import com.entity.OrderStatus;
import com.entity.Orders;
import com.entity.Trolley;
import com.util.nowTime;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.util.Vessel.order;
import static com.util.Vessel.orderCRUD;

public class OrderImpl implements Order {
    opMenuImpl opMenu = new opMenuImpl();

    //
    @Override
    public int confirm(int cId) {
        String sql = "UPDATE orders\n" +
                "SET oStatus = '已下单，待支付'\n" +
                "WHERE cId = ?";
        Object[] objects = new Object[1];
        objects[0] = cId;
        int num = orderCRUD.update(sql,objects);
        return num;
    }

    @Override
    public List<OrderStatus> unPay(int cId) {
        String sql = "SELECT od.oId,me.mId,me.mName,SUM(mNumber) Number,me.mPrice,tId,od.cId,oStatus,mNumber*mPrice Price\n" +
                "FROM order_details AS od\n" +
                "         LEFT JOIN orders AS os ON od.oId=os.oId\n" +
                "         LEFT JOIN menus AS me ON me.mId = od.mId\n" +
                "WHERE os.cId = ? AND oStatus = '已下单，待支付'\n" +
                "GROUP BY me.mId, od.oId, me.mName, mNumber, me.mPrice, tId, od.cId, oStatus, mNumber*mPrice";
        Object[] objects = new Object[1];
        objects[0] = cId;
        ResultSet resultSet = orderCRUD.printAll(sql,objects);
        List<OrderStatus> orderStatusList = new ArrayList<>();
        try{
            while(resultSet.next()){
                OrderStatus orderStatus = new OrderStatus();
                orderStatus.setoId(resultSet.getInt("od.oId"));
                orderStatus.setmId(resultSet.getInt("me.mId"));
                orderStatus.setmName(resultSet.getString("me.mName"));
                orderStatus.setmNumber(resultSet.getInt("Number"));
                orderStatus.setmPrice(resultSet.getDouble("me.mPrice"));
                orderStatus.settId(resultSet.getInt("tId"));
                orderStatus.setcId(resultSet.getInt("od.cId"));
                orderStatus.setoStatus(resultSet.getString("oStatus"));
                orderStatus.setPrice(resultSet.getDouble("Price"));
                orderStatusList.add(orderStatus);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return orderStatusList;
    }

    //确认下单
    @Override
    public int orderMenus(int cId) {
        int num = 0;
        int oId = 0;
        String tStu = "已下单，待支付";
        String orderSQL = "INSERT INTO orders(cId,oTime,oStatus) VALUES(?,?,?)";
        nowTime time = new nowTime();
        String nTimes = time.nTime();
        Object[] orderObjects = new Object[3];
        orderObjects[0] = cId;
        orderObjects[1] = nTimes;
        orderObjects[2] = tStu;
        orderCRUD.update(orderSQL,orderObjects);

        String quSQL = "SELECT oId FROM orders\n" +
                "WHERE cId = ? \n" +
                "  AND oStatus = '已下单，待支付'";
        Object[] quObjects = new Object[1];
        quObjects[0] = cId;
        ResultSet resultSet = orderCRUD.printAll(quSQL,quObjects);
        List<Trolley> trolleyList1 = new ArrayList<>();
        try{
            while(resultSet.next()){
                Trolley trolley = new Trolley();
                trolley.setoId(resultSet.getInt("oId"));
                trolleyList1.add(trolley);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        for (Trolley trolley : trolleyList1){
            oId = trolley.getoId();
        }
        List<Trolley> trolleyList = selectedMealBag(cId);
        //将购物车中菜品转移至订单明细中
        for (Trolley trolley : trolleyList){
            String sql = "INSERT INTO order_details(mId,mNumber,cId,oId) VALUES (?,?,?,?)";
            Object[] objects = new Object[4];
            objects[0] = trolley.getmId();
            objects[1] = trolley.getNumber();
            objects[2] = trolley.getcId();
            objects[3] = oId;
            num = orderCRUD.update(sql,objects);
        }
        //清空餐袋
        String clearSQL = "DELETE FROM trolley WHERE cId = ? AND orderStatus = '已勾选'";
        Object[] clearObject = new Object[1];
        clearObject[0] = cId;
        orderCRUD.update(clearSQL,clearObject);


        return num;
    }

    @Override
    public int choiceOrder(int cId, int tId,int mId) {
        String sql = "UPDATE trolley\n" +
                "SET tStatus = true\n" +
                "WHERE cId = ? \n" +
                "    AND mId = ?\n" +
                "    AND tId = ?";
        Object[] objects = new Object[3];
        objects[0] = cId;
        objects[1] = mId;
        objects[2] = tId;
        int num = orderCRUD.update(sql,objects);
        return num;
    }

    //选择确认下单
    @Override
    public int orderChoice(int cId, int tId, int mId) {
        int num = 0;
        int oId = 0;
        String tStu = "已下单，待支付";
        String orderSQL = "INSERT INTO orders(cId,oTime,oStatus) VALUES(?,?,?)";
        nowTime time = new nowTime();
        String nTimes = time.nTime();
        Object[] orderObjects = new Object[3];
        orderObjects[0] = cId;
        orderObjects[1] = nTimes;
        orderObjects[2] = tStu;
        orderCRUD.update(orderSQL,orderObjects);

        String quSQL = "SELECT oId FROM orders\n" +
                "WHERE cId = ? \n" +
                "  AND oStatus = '已下单，待支付'";
        Object[] quObjects = new Object[1];
        quObjects[0] = cId;
        ResultSet resultSet = orderCRUD.printAll(quSQL,quObjects);
        List<Trolley> trolleyList1 = new ArrayList<>();
        try{
            while(resultSet.next()){
                Trolley trolley = new Trolley();
                trolley.setoId(resultSet.getInt("oId"));
                trolleyList1.add(trolley);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        for (Trolley trolley : trolleyList1){
            oId = trolley.getoId();
        }
        List<Trolley> trolleyList = opMenu.vsMealBag(cId,tId);
        String sql = "INSERT INTO order_details(mId,mNumber,cId,oId)\n" +
                "SELECT tro.mId,tro.mNumber,tro.cId,? \n" +
                "FROM trolley tro\n" +
                "WHERE cId = ? AND tro.tStatus = true;";
        Object[] objects = new Object[2];
        objects[0] = oId;
        objects[1] = cId;
        orderCRUD.update(sql,objects);
        //清除已经下单的菜品
        String clearSQL = "DELETE FROM trolley WHERE cId = ? AND tStatus = true";
        Object[] clearObject = new Object[1];
        clearObject[0] = cId;
        orderCRUD.update(clearSQL,clearObject);
        return num;
    }

    //已支付订单信息
    @Override
    public List<OrderStatus> paid(int cId) {
        String sql = "SELECT od.oId,me.mId,me.mName,SUM(mNumber) Number,me.mPrice,tId,od.cId,oStatus,mNumber*mPrice Price\n" +
                "FROM order_details AS od\n" +
                "LEFT JOIN orders AS os ON od.oId=os.oId\n" +
                "LEFT JOIN menus AS me ON me.mId = od.mId\n" +
                "WHERE os.cId = ? AND oStatus = '已支付'\n" +
                "GROUP BY me.mId, od.oId, me.mName, mNumber, me.mPrice, tId, od.cId, oStatus, mNumber*mPrice;";
        Object[] objects =new Object[1];
        objects[0] = cId;
        ResultSet resultSet = orderCRUD.printAll(sql,objects);
        List<OrderStatus> orderStatusList = new ArrayList<>();
        try{
            while(resultSet.next()){
                OrderStatus orderStatus = new OrderStatus();
                orderStatus.setoId(resultSet.getInt("od.oId"));
                orderStatus.setmId(resultSet.getInt("me.mId"));
                orderStatus.setmName(resultSet.getString("me.mName"));
                orderStatus.setmNumber(resultSet.getInt("Number"));
                orderStatus.setmPrice(resultSet.getDouble("me.mPrice"));
                orderStatus.settId(resultSet.getInt("tId"));
                orderStatus.setcId(resultSet.getInt("od.cId"));
                orderStatus.setoStatus(resultSet.getString("oStatus"));
                orderStatus.setPrice(resultSet.getDouble("Price"));
                orderStatusList.add(orderStatus);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return orderStatusList;
    }

    //勾选菜品
    @Override
    public int select(int cId, int mId,int tId) {
        int num = 0;
        String sql = "UPDATE trolley\n" +
                "SET orderStatus = '已勾选'\n" +
                "WHERE cId = ?\n" +
                "    AND mId = ?\n" +
//                "    AND orderStatus = '未勾选'" +
                "    AND tId = ?";
        Object[] objects = new Object[3];
        objects[0] = cId;
        objects[1] = mId;
        objects[2] = tId;
        num = orderCRUD.update(sql,objects);
        return num;
    }

    //全勾选
    @Override
    public int selectAll(int cId, int tId) {
        int num = 0;
        String sql = "UPDATE trolley\n" +
                "SET orderStatus = '已勾选'\n" +
                "WHERE cId = ?\n" +
//                "    AND orderStatus = '未勾选'" +
                "    AND tId = ?";
        Object[] objects = new Object[2];
        objects[0] = cId;
        objects[1] = tId;
        num = orderCRUD.update(sql,objects);
        return num;
    }

    //全取消勾选
    @Override
    public int cancelAll(int cId, int tId) {
        int num = 0;
        String sql = "UPDATE trolley\n" +
                "SET orderStatus = '未勾选'\n" +
                "WHERE cId = ?\n" +
                "    AND orderStatus = '已勾选'" +
                "    AND tId = ?";
        Object[] objects = new Object[2];
        objects[0] = cId;
        objects[1] = tId;
        num = orderCRUD.update(sql,objects);
        return num;
    }

    //选择取消勾选
    @Override
    public int cancel(int cId, int mId, int tId) {
        int num = 0;
        String sql = "UPDATE trolley\n" +
                "SET orderStatus = '未勾选'\n" +
                "WHERE cId = ?\n" +
                "    AND mId = ?\n" +
                "    AND tId = ?";
        Object[] objects = new Object[3];
        objects[0] = cId;
        objects[1] = mId;
        objects[2] = tId;
        num = orderCRUD.update(sql,objects);
        return num;
    }

    //改变菜品的勾选状态
    @Override
    public int alter(int cId, int mId, int tId) {
        String status = dishSelect(cId,mId,tId);
        int num = 0;
        String statusOrder = null;
        if (status.equals("已勾选")){
            statusOrder = "未勾选";
        } else if (status.equals("未勾选")) {
            statusOrder = "已勾选";
        }
        String sql = "UPDATE trolley\n" +
                "SET orderStatus = ?\n" +
                "WHERE cId = ?\n" +
                "    && mId = ?\n" +
                "    && tId = ?";
        Object[] objects = new Object[4];
        objects[0] = statusOrder;
        objects[1] = cId;
        objects[2] = mId;
        objects[3] = tId;
        num = orderCRUD.update(sql,objects);
        return num;
    }

    //确定菜品的勾选状态
    @Override
    public String dishSelect(int cId, int mId, int tId) {
        String sql = "SELECT orderStatus\n" +
                "FROM trolley\n" +
                "WHERE cId = ? \n" +
                "          && mId = ? \n" +
                "          && tId = ?";
        Object[] objects = new Object[3];
        objects[0] = cId;
        objects[1] = mId;
        objects[2] = tId;
        ResultSet resultSet = orderCRUD.printAll(sql,objects);
        String status = null;
        try{
            while (resultSet.next()){
                status = resultSet.getString("orderStatus");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return status;
    }

    //选择已勾选的菜品
    @Override
    public List<Trolley> selectedMealBag(int cId) {
        String sql = "SELECT me.mId,me.mName,SUM(tro.mNumber) Number,me.mPrice,tro.tId,SUM(me.mPrice * mNumber) aPrice,tro.orderStatus\n" +
                "FROM trolley AS tro\n" +
                "LEFT JOIN menus AS me ON tro.mId = me.mId\n" +
                "WHERE cId = ? AND tro.tStatus = false AND tro.orderStatus = '已勾选'\n" +
                "GROUP BY me.mId,mName,mPrice,tro.tId,tro.orderStatus";
        Object[] objects = new Object[1];
        objects[0] = cId;
        ResultSet resultSet = orderCRUD.printAll(sql,objects);
        List<Trolley> TrolleyList = new ArrayList<>();
        try{
            while(resultSet.next()){
                Trolley trolley = new Trolley();
                trolley.setmId(resultSet.getInt("me.mId"));
                trolley.setName(resultSet.getString("me.mName"));
                trolley.setNumber(resultSet.getInt("Number"));
                trolley.setPrice(resultSet.getDouble("me.mPrice"));
                trolley.setaPrice(resultSet.getDouble("aPrice"));
                trolley.settId(resultSet.getInt("tro.tId"));
                trolley.setOrderStatus(resultSet.getString("tro.orderStatus"));
                TrolleyList.add(trolley);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return TrolleyList;
    }

    //评论
    @Override
    public int comment(int cId, int oId,String comments) {
        int num = 0;
        String sql = "UPDATE orders\n" +
                "SET comment = ?\n" +
                "WHERE cId = ?\n" +
                "AND oId = ?\n" +
                "AND comment IS NULL\n" +
                "AND oStatus = '已支付'";
        Object[] objects = new Object[3];
        objects[0] = comments;
        objects[1] = cId;
        objects[2] = oId;
        num = orderCRUD.update(sql,objects);
        return num;
    }

    //已支付订单order查询
    @Override
    public List<Orders> orderPaid(int cId) {
        String sql = "SELECT oId,tId,cId,oTime,oStatus,comment\n" +
                "FROM orders\n" +
                "WHERE cId = ?";
        Object[] objects = new Object[1];
        objects[0] = cId;
        ResultSet resultSet = orderCRUD.printAll(sql,objects);
        List<Orders> ordersList = new ArrayList<>();
        try{
            while (resultSet.next()){
                Orders order = new Orders();
                order.setoId(resultSet.getInt("oId"));
                order.settId(resultSet.getInt("tId"));
                order.setcId(resultSet.getInt("cId"));
                order.setoTime(resultSet.getString("oTime"));
                order.setoStatus(resultSet.getString("oStatus"));
                order.setComment(resultSet.getString("comment"));
                ordersList.add(order);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return ordersList;
    }

    //订单评论
    @Override
    public List<Orders> orderComments() {
        String sql = "SELECT comment\n" +
                "FROM orders\n" +
                "WHERE comment IS NOT NULL";
        Object[] objects = new Object[1];
        objects[0] = null;
        ResultSet resultSet = orderCRUD.printAll(sql,objects);
        List<Orders> ordersList = new ArrayList<>();
        try{
            while (resultSet.next()){
                Orders order = new Orders();
                order.setComment(resultSet.getString("comment"));
                ordersList.add(order);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return ordersList;
    }
}